<?php
namespace addon\wechat_config\logic;
// +----------------------------------------------------------------------
// | Author: xiaojun <920668625@qq.com>
// +----------------------------------------------------------------------
use app\common\logic\LogicBase;
use think\Db;
use \tpfcore\Core;
use addon\wechat_material\logic\WechatMaterial;

class ReceiveText extends LogicBase
{
	public function receiveTextFunc($object){

		$content = '';
        $keyword = trim($object->Content);
        
        if(TO_KF_KEYWORD){
        	if(strstr($keyword, TO_KF_KEYWORD)){
	        	//转发到客服系统
	            return WechatFunc::transmitService($object);
	        }
        }

		$replydata = WechatReceive::materialKeywordList($keyword);

		if($replydata)
		{
			switch(WECHAT_REPLY_TYPE)
            {
                case 'image':
                	$content = array();
                    $content = ['MsgType'=>'image','MediaId'=>$replydata['media_id']];
                    break;
                case 'voice':
                	$content = array();
                    $content = [
						'MsgType'     => 'voice',
						'Title'       => $replydata['name'],
						'Description' => '来自：'.WECHAT_NAME,
						'MusicUrl'    => WechatFunc::format_url($replydata['local_url']),
						'HQMusicUrl'  => WechatFunc::format_url($replydata['local_url'])
                    ];
                    break;
                case 'video':
                    $content = array();
                    $content = [
						'MsgType'      => 'video',
						'MediaId'      => $replydata['media_id'],
						'ThumbMediaId' => '',
						'Title'        => $replydata['name'],
						'Description'  => $replydata['description']
                    ];
                    break;
                case 'news':
					$content = array();
                    $content[] = [
						'MsgType'     => 'news',
						'Title'       => $replydata['title'],
						'Description' => $replydata['description'],
						'PicUrl'      => WechatFunc::format_url($replydata['pic_url']),
						'Url'         => $replydata['url']?$replydata['url']:$replydata['content_source_url']
                    ];
                    $son_data = (new WechatMaterial())->newsMaterialList(['group_id'=>$replydata['id']],0,'id asc');
                    foreach ($son_data as $key => $value) {
                    	$content[] = [
							'MsgType'     => 'news',
							'Title'       => $value['title'],
							'Description' => $value['description'],
							'PicUrl'      => WechatFunc::format_url($value['pic_url']),
							'Url'         => $value['url']?$value['url']:$value['content_source_url']
                    	];
                    }
                    break;
                case 'text':
                    $content = '';
                    $content = strip_tags($replydata,'<a>');

                    preg_match_all('/\[([0-9a-z_]+)\]/i',$content,$matches);
                    foreach($matches[1] as $emoji){
						$info    = WechatFunc::getEemoj($emoji);
						//处理表情
						$content = str_replace('['.$emoji.']',WechatFunc::bytesToEmoji($info['utf16']),$content);
                    }
                break;
            }
		}else{
			//自动回复模式
			if(WECHAT_ROBOT_ON)
			{
				//图灵机器人
				$content = WechatRobot::tuling($keyword);
			}else{
				//默认回复内容
				switch(WECHAT_AUTO_MSG_TYPE)
				{
					case 'image':
						$content = array();
						$content = ['MsgType'=>'image','MediaId'=>WECHAT_AUTO_MSG_VALUE];
						break;
					case 'voice':
						$info = (new WechatMaterial())->getVoiceMaterialByMediaId(WECHAT_AUTO_MSG_VALUE);

						$content = array();
						$content = [
							'MsgType'     => 'voice',
							'Title'       => $info['name'],
							'Description' => '来自：'.WECHAT_NAME,
							'MusicUrl'    => WechatFunc::format_url($info['local_url']),
							'HQMusicUrl'  => WechatFunc::format_url($info['local_url'])
						];
						break;
					case 'video':
						$info = (new WechatMaterial())->getVideoMaterialByMediaId(WECHAT_AUTO_MSG_VALUE);
						$content = array();
						$content = [
							'MsgType'      => 'video',
							'MediaId'      => WECHAT_AUTO_MSG_VALUE,
							'ThumbMediaId' => '',
							'Title'        => $info['name'],
							'Description'  => $info['description']
						];
						break;
					case 'news':
						$WechatMaterial = new WechatMaterial();
						$info    = $WechatMaterial->getNewsMaterialByMediaId(WECHAT_AUTO_MSG_VALUE);
						$content = array();
                    	$content[] = [
							'MsgType'     => 'news',
							'Title'       => $info['title'],
							'Description' => $info['description'],
							'PicUrl'      => WechatFunc::format_url($info['pic_url']),
							'Url'         => $info['url']?$info['url']:$info['content_source_url']
                    	];

						$son_data = $WechatMaterial->newsMaterialList(['group_id'=>$info['id']],0,'id asc');
	                    foreach ($son_data as $key => $value) {
	                    	$content[] = [
								'MsgType'     => 'news',
								'Title'       => $value['title'],
								'Description' => $value['description'],
								'PicUrl'      => WechatFunc::format_url($value['pic_url']),
								'Url'         => $value['url']?$value['url']:$value['content_source_url']
	                    	];
	                    }
						break;
					case 'text':
						$content = '';
						$content = strip_tags(WECHAT_AUTO_MSG_TEXT,'<a>');
						preg_match_all('/\[([0-9a-z_]+)\]/i',$content,$matches);
						foreach($matches[1] as $emoji)
						{
							$info    = WechatFunc::getEemoj($emoji);
							$content = str_replace('['.$emoji.']',WechatFunc::bytesToEmoji($info['utf16']),$content);
						}
						break;
                }
			}
		}
		
        return WechatReceive::DisposeContent($object,$content);
	}
}